package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.api.langdu.vo.FeedBackVo;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.SysFeedbackT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.entity.YdTreeInfoT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.SysFeedbackTService;
import com.jshoperxms.service.YdRecordRdTService;
import com.jshoperxms.service.YdTreeInfoTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2018/10/5.
 * Des:
 */
@RestController
@RequestMapping("/api/minecenter")
public class MineCenterController extends BaseTController {
    @Resource
    private YdRecordRdTService ydRecordRdTService;

    @Resource
    private YdTreeInfoTService ydTreeInfoTService;

    @Resource
    private SysFeedbackTService sysFeedbackTService;

    @Resource
    private MemberTService memberTService;


    /**
     * 获取我的阅读记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMineYdRecords",method= RequestMethod.POST)
    public ResponseEntity findMineYdRecordsList(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
        int currentPage=rq.getStart();
        int lineSize=rq.getLength();
        int records=this.ydRecordRdTService.count(YdRecordRdT.class,criterion).intValue();
        List<YdRecordRdT> list=this.ydRecordRdTService.findByCriteriaByPage(YdRecordRdT.class,criterion, Order.desc(TagWords.CREATETIME),currentPage,lineSize);
        rq.setRecordsTotal(records);
        responsedata.put("results",list);
        responsedata.put("start",rq.getNextPageNo());
        responsedata.put("totalPage",rq.getTotalPage());
        responsedata.put("totalrecords",records);
        responsedata.put(TagWords.SUCCESS,true);
        return Response.success(responsedata);
    }


    /**
     * 获取我的森林
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMineTrees",method= RequestMethod.POST)
    public ResponseEntity findMineTrees(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())){
            Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add( Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState()));
            List<YdTreeInfoT>beanlist=this.ydTreeInfoTService.findByCriteria(YdTreeInfoT.class,criterion,Order.asc(TagWords.CREATETIME));
            responsedata.put(TagWords.BEANLIST,beanlist);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
        }
        return Response.success(responsedata);

    }


    /**
     * 保存客户反馈意见
     * @param rq
     * @return
     */
    @RequestMapping(value="/saveFeedBack",method= RequestMethod.POST)
    public ResponseEntity saveFeedBack(@RequestBody FeedBackVo rq) {
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())) {
            Criterion criterion=Restrictions.eq("wxopenid",rq.getWxopenid());
            MemberT memberT=this.memberTService.findOneByCriteria(MemberT.class,criterion);
            if(memberT!=null){
                SysFeedbackT bean=new SysFeedbackT();
                bean.setId(PrimaryKey.get().toString());
                bean.setContent(rq.getContent());
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setDevicetype("4");//小程序
                bean.setStoreid("0");
                bean.setCreatetime(DateTools.getSystemTimeLong());
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                bean.setVersiont(1);
                bean.setMemberid(memberT.getId());
                bean.setFeedbacksource("2");
                bean.setFeedbackpurpose("1");
                bean.setProject("YDNS");
                bean.setStorename(StaticKey.EMPTY);
                this.sysFeedbackTService.save(bean);
                responsedata.put(TagWords.SUCCESS,true);
            }
        }else{
            responsedata.put(TagWords.SUCCESS,false);
        }
        return Response.success(responsedata);
    }


    /**
     * 更新朗读内容标题
     * @param rq
     * @return
     */
    @RequestMapping(value="/updateRdTitle",method= RequestMethod.POST)
    public ResponseEntity updateRdTitle(@RequestBody YdRecordRdT rq){
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getId())&&StringUtils.isNotBlank(rq.getTitle())) {
            YdRecordRdT bean=this.ydRecordRdTService.findByPK(YdRecordRdT.class,rq.getId());
            if(bean!=null){
                bean.setTitle(rq.getTitle());
                bean.setVersiont(bean.getVersiont()+1);
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                this.ydRecordRdTService.update(bean);
                responsedata.put(TagWords.SUCCESS,true);
            }
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }

    /**
     * 删除朗读记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/deleteRecord",method= RequestMethod.POST)
    public ResponseEntity deleteRecord(@RequestBody YdRecordRdT rq){
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getId())) {
            YdRecordRdT bean=this.ydRecordRdTService.findByPK(YdRecordRdT.class,rq.getId());
            if(bean!=null){
                bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                bean.setVersiont(bean.getVersiont()+1);
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                this.ydRecordRdTService.update(bean);
                responsedata.put(TagWords.SUCCESS,true);
            }
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }



}
